using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using CCWin;
using ERP_SaaS_WinForm.登录.Common;

namespace ERP_SaaS_WinForm.显示
{
    public partial class MainForm : CCSkinMain
    {
        public MainForm()
        {
            InitializeComponent();
            InitializeMainForm();
            
            // 添加键盘事件处理
            this.KeyPreview = true;
        }


        private void InitializeMainForm()
        {
            // 设置窗体标题
            this.Text = "ERP管理系统 - 主页";
            this.WindowState = FormWindowState.Maximized;
            
            // 设置窗体图标
            SetFormIcon();
            
            // 检查登录状态
            if (!TokenDto.IsLoggedIn)
            {
                MessageBox.Show("请先登录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.Close();
                return;
            }

            // 显示欢迎信息
            lblWelcome.Text = $"欢迎您，{TokenDto.Name}！";
            
            // 设置链接样式
            SetupLinkStyles();
            
            // 设置卡片样式
            SetupCardStyles();
        }

        private void SetupLinkStyles()
        {
            // 为所有卡片设置鼠标悬停效果
            var cards = new[] 
            {
                cardMaterial, cardProduct, cardProcess, cardProcurement, cardSales, cardProduction
            };

            foreach (var card in cards)
            {
                card.Cursor = Cursors.Hand;
                card.MouseEnter += (s, e) => {
                    var panel = s as Panel;
                    if (panel != null)
                    {
                        panel.BackColor = Color.FromArgb(245, 245, 245);
                        panel.BorderStyle = BorderStyle.FixedSingle;
                    }
                };
                card.MouseLeave += (s, e) => {
                    var panel = s as Panel;
                    if (panel != null)
                    {
                        panel.BackColor = Color.White;
                        panel.BorderStyle = BorderStyle.None;
                    }
                };
            }

            // 为退出登录设置特殊样式
            lblLogout.Cursor = Cursors.Hand;
            lblLogout.MouseEnter += (s, e) => {
                lblLogout.ForeColor = Color.LightGray;
                lblLogout.Font = new Font(lblLogout.Font, FontStyle.Underline);
            };
            lblLogout.MouseLeave += (s, e) => {
                lblLogout.ForeColor = Color.White;
                lblLogout.Font = new Font(lblLogout.Font, FontStyle.Regular);
            };
        }

        private void SetupCardStyles()
        {
            // 为所有卡片添加圆角和阴影效果
            var cards = new[] 
            {
                cardMaterial, cardProduct, cardProcess, cardProcurement, cardSales, cardProduction
            };

            foreach (var card in cards)
            {
                // 设置圆角边框
                card.Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, card.Width, card.Height, 10, 10));
                
                // 添加阴影效果（通过边框实现）
                card.BorderStyle = BorderStyle.None;
            }
        }

        [System.Runtime.InteropServices.DllImport("Gdi32.dll")]
        private static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);

        private void ShowAlert(string message, string caption)
        {
            MessageBox.Show(message, caption, MessageBoxButtons.OK, 
                caption == "成功" ? MessageBoxIcon.Information 
                : caption == "提示" ? MessageBoxIcon.Information 
                : MessageBoxIcon.Error);
        }
        //物料管理
        private void lblMaterialManagement_Click(object sender, EventArgs e)
        {
            try
            {
                var salesForm = new ERP_SaaS_WinForm.物料管理.MainMaterialForm();
                salesForm.Show();
            }
            catch (Exception ex)
            {
                ShowAlert($"打开物料管理模块失败: {ex.Message}", "错误");
            }
        }
        /// <summary>
        /// 生产工单+报工+工序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lblProcessManagement_Click(object sender, EventArgs e)
        {
            try
            {
                var procurementForm = new ERP_SaaS_WinForm.生产工单_报工_工序.ProcessForm();
                procurementForm.Show();
            }
            catch (Exception ex)
            {
                ShowAlert($"打开生产工单+报工+工序失败: {ex.Message}", "错误");
            }
        }
        // 采购管理
        private void lblProcurementManagement_Click(object sender, EventArgs e)
        {
            try
            {
                // 打开现有的采购管理窗体
                var procurementForm = new ERP_SaaS_WinForm.采购管理.Quality();
                procurementForm.Show();
            }
            catch (Exception ex)
            {
                ShowAlert($"打开采购管理模块失败: {ex.Message}", "错误");
            }
        }

        // 销售管理
        private void lblSalesManagement_Click(object sender, EventArgs e)
        {
            try
            {
                // 打开现有的销售管理窗体
                var salesForm = new ERP_SaaS_WinForm.销售.Sale();
                salesForm.Show();
            }
            catch (Exception ex)
            {
                ShowAlert($"打开销售管理模块失败: {ex.Message}", "错误");
            }
        }
        // 生产计划+派单+车间
        private void lblProductionManagement_Click(object sender, EventArgs e)
        {
            try
            {
                var navigationForm = new ERP_SaaS_WinForm.生产计划_派单_车间.NavigationForm();
                navigationForm.Show();
            }
            catch (Exception ex)
            {
                ShowAlert($"打开生产计划+派单+车间模块失败: {ex.Message}", "错误");
            }
        }
        // 退出登录
        private void lblLogout_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要退出登录吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                TokenDto.Clear();
                ShowAlert("已退出登录", "提示");
                this.Close();
            }
        }

        /// <summary>
        /// 设置窗体图标
        /// </summary>
        private void SetFormIcon()
        {
            try
            {
                // 尝试多个可能的路径
                string[] possiblePaths = {
                    Path.Combine(Application.StartupPath, "..", "..", "Images", "s.ico")
                };
                
                string iconPath = null;
                foreach (string path in possiblePaths)
                {
                    Console.WriteLine($"MainForm检查图标路径: {path}");
                    if (File.Exists(path))
                    {
                        iconPath = path;
                        Console.WriteLine($"MainForm找到图标文件: {path}");
                        break;
                    }
                }
                
                if (iconPath != null)
                {
                    Console.WriteLine("MainForm正在加载图标...");
                    this.Icon = new System.Drawing.Icon(iconPath);
                    Console.WriteLine("MainForm图标加载成功！");
                }
                else
                {
                    Console.WriteLine("MainForm图标文件不存在，使用默认图标...");
                    // 如果找不到s.ico文件，使用系统默认图标
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"MainForm设置窗体图标失败: {ex.Message}");
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void lblProductIcon_Click(object sender, EventArgs e)
        {
            try
            {
                // 打开现有的产品库存窗体
                var procurementForm = new ERP_SaaS_WinForm.登录.MainProductMes();
                procurementForm.Show();
            }
            catch (Exception ex)
            {
                ShowAlert($"打开产品模块失败: {ex.Message}", "错误");
            }
        }

        private void cardProduct_Paint(object sender, PaintEventArgs e)
        {

        }

        private void cardSales_Paint(object sender, PaintEventArgs e)
        {

        }
    }
} 